﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using WQLWebAPI.Domain.Abstract;
using WQLWebAPI.Domain.Entities;

namespace WQLWebAPI.EntityFramework
{
    public class BaseRepository<TEntity > : IBaseRepository<TEntity> where TEntity : Entity
    {
        protected readonly ApplicationDbContext DbContext;
        protected readonly DbSet<TEntity> Db;
        public BaseRepository(ApplicationDbContext context)
        {
            DbContext = context;
            Db = DbContext.Set<TEntity>();
        }

        public void Add(TEntity obj)
        {
            Db.Add(obj);
        }

        public async Task AddAsync(TEntity obj)
        {
            await Db.AddAsync(obj);
        }
        public async Task AddRangeAsync(IEnumerable<TEntity> entities)
        {
            await Db.AddRangeAsync(entities);
        }
        public IQueryable<TEntity> GetAll()
        {
            return Db;
        }

        public TEntity GetById(int id)
        {
            return Db.Find(id);
        }

        public async Task<TEntity> GetByIdAsync(int id)
        {
            return await Db.FindAsync(id);
        }

        public void Remove(TEntity entity)
        {
            Db.Remove(entity);
        }
        public void RemoveRange(IEnumerable<TEntity> entity)
        {
            Db.RemoveRange(entity);
        }
        public int SaveChanges()
        {
            return DbContext.SaveChanges();
        }

        public async Task<int> SaveChangesAsync()
        {
            return await DbContext.SaveChangesAsync();
        }

        public void Update(TEntity obj)
        {
            
            Db.Update(obj);
        }
    }
}
